package com.aiyige.page.my.order.dialog;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.OrderEntity;
import com.aiyige.model.eshop.Order;
import com.aiyige.model.request.ConfirmClassedRequest;
import com.aiyige.setup.model.Setup;
import com.aiyige.utils.ErrorUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.RetrofitUtil;
import com.aiyige.utils.UIHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 确认上课弹出框
 */

public class ConfirmInClassDialog extends DialogFragment {
    Unbinder unbinder;
    String message;
    @BindView(R.id.contentTv)
    TextView contentTv;

    public Listener getListener() {
        return listener;
    }

    public ConfirmInClassDialog setListener(Listener listener) {
        this.listener = listener;
        return this;
    }

    Listener listener;
    String orderId;

    public static ConfirmInClassDialog newInstance(String orderId) {
        ConfirmInClassDialog fragment = new ConfirmInClassDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.CommonDialogTheme);
        fragment.orderId = orderId;
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_confirm_in_class_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
//        contentTv.setText(message);
        contentTv.setMovementMethod(ScrollingMovementMethod.getInstance());
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = Resources.getSystem().getDisplayMetrics().widthPixels - UIHelper.dip2px(75);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.okBtn,R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.okBtn:
                request();
                break;
            case R.id.cancelBtn:
                dismiss();
                break;
        }
    }

    private void request() {
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        ConfirmClassedRequest.newBuilder().orderId(orderId).build().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (!response.isSuccessful()) {
                        throw new Exception(ErrorUtil.extractMessageFromErrorResponse(response));
                    }
                    String json = RetrofitUtil.getResponseBodyAsString(response);
                    Order order = JsonUtil.toObject(json, Order.class);
                    if (order != null && listener != null) {
                        OrderEntity orderEntity = new OrderEntity(order);
                        listener.onConfirmClassed(orderEntity);
                        dismiss();
                    }
                } catch (Exception e) {
                    onFailure(call, e);
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public interface Listener {
        void onConfirmClassed(OrderEntity orderEntity);
    }

}
